package com.whz.workmanageol.security;

import com.whz.workmanageol.entity.User;
import com.whz.workmanageol.service.LoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service("userDetailsService")
public class CustomUserDetailsService implements UserDetailsService {
    private final static Logger logger = LoggerFactory.getLogger(CustomUserDetailsService.class);
    @Autowired
    private LoginService loginService;

    @Override
    public UserDetails loadUserByUsername(String userid) throws UsernameNotFoundException {
        User user = loginService.findByUserid(userid);
        logger.info("userid: {}", userid);
        // 用户名被置空则代表用户名不存在
        if("".equals(user.getUserid())) {
            throw new UsernameNotFoundException(userid);
        }
        // 自定义授权策略
        return new MyUserPrincipal(user);
    }
}
